package com.scau.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.scau.dto.CBECReturn;
import com.scau.dto.SignForm;
import com.scau.handleException.ResultCode;
import com.scau.pojo.EWallet;
import com.scau.pojo.SystemUser;
import com.scau.service.IEWalletService;
import com.scau.service.ISystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;

@Controller
@CrossOrigin
@RequestMapping("")
public class SignController {
    @Autowired
    ISystemUserService systemUserService;
    @Autowired
    IEWalletService walletService;


    @RequestMapping("/user/toSignPage")
    public String toSignPage(){
        return "UserSign";
    }


    //注册事件
    @RequestMapping(value = "/user/sign",method = RequestMethod.POST)
    @ResponseBody
    public CBECReturn signin(@RequestBody SignForm signForm){

        System.out.println("==================前端提交数据：" + signForm);
        SystemUser systemUser = signForm.getSystemUser();
        EWallet wallet = signForm.getWallet();

        String username = systemUser.getUsername();
        SystemUser user = systemUserService.selectUser(username);
        EWallet walletTemp = walletService.getWallet(wallet.getPayId());
        System.out.println(signForm);

        CBECReturn result;
        if(user == null && walletTemp==null){
            //初始化菜单列表
            if(systemUser.getType().equals("1"))
                systemUser.setMenu("000000111111");
            else
                systemUser.setMenu("111111000000");
            systemUserService.addUser(systemUser);
            //初始化余额
            wallet.setBalance(0.0);
            wallet.setUsername(username);
            walletService.addWallet(wallet);
            return CBECReturn.build(ResultCode.SUCCESS);
//            result = CBECReturn.build(200, "
        }
        return  CBECReturn.build(ResultCode.REQUESTBODY_ERROR);
    }


}
